//
//  SLCAVectorDrawController.m
//  Objective-C
//
//  Created by S_Line on 2018/7/4.
//  Copyright © 2018年 S_Line. All rights reserved.
//

#import "SLCAVectorDrawController.h"

@interface SLCAVectorDrawController ()

@property (nonatomic, weak) CAShapeLayer *shapeLayer;
@property (nonatomic, strong) UIBezierPath *path;

@end

@implementation SLCAVectorDrawController

- (void)initNavBar {
    self.navTitle = @"矢量图形";
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initNavBar];
    
    [self initView];
}

- (void)initView {
    self.path = [[UIBezierPath alloc] init];
    
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    self.shapeLayer = shapeLayer;
    shapeLayer.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
    shapeLayer.fillColor = [UIColor clearColor].CGColor;
    shapeLayer.strokeColor = [UIColor redColor].CGColor;
    shapeLayer.lineJoin = kCALineJoinRound;
    shapeLayer.lineCap = kCALineCapRound;
    shapeLayer.lineWidth = 5;
    [self.view.layer addSublayer:shapeLayer];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    // get the start point
    CGPoint point = [[touches anyObject] locationInView:self.view];
    
    // move the path drawing cursor to the starting point
    [self.path moveToPoint:point];
}

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    // get the current point
    CGPoint point = [[touches anyObject] locationInView:self.view];
    
    // add a new line segment to our path
    [self.path addLineToPoint:point];
    
    // update the layer with a copy of the path
    self.shapeLayer.path = self.path.CGPath;
}

@end
